using System.Collections;
using System.Collections.Generic;
using UnityEditor.Tilemaps;
using UnityEngine;
using static Unity.Collections.AllocatorManager;
using static UnityEditor.PlayerSettings;

namespace UnityEditor
{



    [CustomGridBrush(true, false, false, "CustomBrush")]
    [CreateAssetMenu(fileName = "CustomBrush", menuName = "Terraria/new CustomBrush")]
    public class CustomBrush : GridBrush
    {
        public List<TileClass> tiles;

        public override void Paint(GridLayout gridLayout, GameObject brushTarget, Vector3Int position)
        {
            base.Paint(gridLayout, brushTarget, position);

        }


        public override void BoxFill(GridLayout gridLayout, GameObject brushTarget, BoundsInt position)
        {

            if (Application.isPlaying)
            {
                foreach (var pos in position.allPositionsWithin)
                {
                    BrushCell cell = cells[GetCellIndexWrapAround(pos.x, pos.y, pos.z)];
                    if (cell.tile is CustomTile)
                    {
                        int id = ((CustomTile)cell.tile).blockId;
                        TerrainManager.Instance.PlaceTile(tiles[id], pos.x, pos.y);
                    }

                }
            }


            else
            {
                base.BoxFill(gridLayout, brushTarget, position);

            }

        }




        public override void Erase(GridLayout gridLayout, GameObject brushTarget, Vector3Int position)
        {
            if (Application.isPlaying)
            {
                int layer = 0;

                Layers layers = Layers.Addons;


                switch (brushTarget.name)
                {
                    case "Tilemap_Addons":
                        layers = Layers.Addons;
                        break;
                    case "Tilemap_Background":
                        layers = Layers.Background;
                        break;
                    case "Tilemap_Ground":
                        layers = Layers.Ground;
                        break;
                    case "Tilemap_Liquid":
                        layers = Layers.Liquid;
                        break;
                    default:
                        layer = -1;
                        break;
                }
                if (layer == 0)
                {
                    layer = (int)layers;
                }

                TerrainManager.Instance.RemoveTile(layer, position.x, position.y);
            }
            else
            {
                base.Erase(gridLayout, brushTarget, position);

            }

        }
    }






    [CustomEditor(typeof(CustomBrush))]
    public class CustomBrushEditor : GridBrushEditor
    {

        public override void OnPaintSceneGUI(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing)
        {
            base.OnPaintSceneGUI(gridLayout, brushTarget, position, tool, executing);

            string viewTxt = "Pos:" + new Vector2(position.x, position.y);

            if (position.size.x > 1 || position.size.y > 1)
            {
                viewTxt += ",Size:" + new Vector2Int(position.size.x, position.size.y);
            }

            Handles.Label(new Vector3(position.x, position.y), viewTxt);
        }

    }



}

